<?php

namespace app\index\controller;
use think\Db;
use think\Cache;

class Login {
    public function login() {
        //return "hahah";
        $userName = $_POST['username'];
        $userPwd = $_POST['userpwd'];
        $result = Db::table("tp_user")->where("user_name",$userName)->find();
        if($result==null) {
            return "用户不存在";
        }
        else {
            if($userPwd != $result['user_pwd']) {
                return "密码错误";
            }
            else {
               // echo "登陆成功！";
                //dump($result);
                session_start();
                $_SESSION["username"]=$userName;
                header("Refresh:0;url=/tp5/public/html/home.html");

            }
        }
    }

    public function register() {
        //echo "nihaoajdlkfjladjfl";
        //header("Location:http://localhost/tp5/public/html/home.html");
        $userName = $_POST['username'];
        $userPwd = $_POST['userpwd'];
        $user = [
            "user_name" => $userName,
            "user_pwd" => $userPwd,
        ];
        $result = Db::table("tp_user")->where("user_name",$userName)->find();
        if($result==null) {
            if(Db::table("tp_user")->insert($user)) {
                echo "注册成功";
                header("Refresh:0;url=/tp5/public/html/login.html");
            }
            else {
                return "添加用户失败";
            }
        }
        else {
            return "用户名已存在";
        }
    }
    public function test() {
        echo "this is test";
    }

}